//
//  movieViewController.m
//  timemovie
//
//  Created by Yiqiao on 15/7/17.
//  Copyright (c) 2015年 Yiqiao. All rights reserved.
//

#import "movieViewController.h"
#import "MObj.h"
#import "movieTableViewCell.h"
#import "common.h"
#import "postView.h"
#define w [UIScreen mainScreen].bounds.size.width
#define h [UIScreen mainScreen].bounds.size.height
@interface movieViewController (){
    BOOL isLeft;
}

@end

@implementation movieViewController

#pragma mark - create area

- (void)_createPosterView{
    
    _postView = [[postView alloc]initWithFrame:self.view.bounds];

    [self.view addSubview:_postView];
    
}

- (void)_createMovieTable{
    
    movieTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, w,h-64-49 )];
    movieTable.delegate = self;
    movieTable.dataSource = self;
    movieTable.hidden = YES;
    [self.view addSubview:movieTable];
    UINib *nib = [UINib nibWithNibName:@"movieTableViewCell" bundle:[NSBundle mainBundle]];
    [movieTable registerNib:nib forCellReuseIdentifier:@"cell1"];
    movieTable.backgroundColor = [UIColor clearColor];
    movieTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

- (void)_createNav{
    
    UIView *flipView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.tag = 1;
    [btn1 addTarget:self action:@selector(flipAct:) forControlEvents:UIControlEventTouchUpInside];
    [btn1 setImage:[UIImage imageNamed:@"list_home"] forState:UIControlStateNormal];
    [btn1 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    [btn1 sizeToFit];
    [flipView addSubview:btn1];
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.tag = 2;
    [btn2 addTarget:self action:@selector(flipAct:) forControlEvents:UIControlEventTouchUpInside];
    [btn2 setImage:[UIImage imageNamed:@"poster_home"] forState:UIControlStateNormal];
    [btn2 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    [btn2 sizeToFit];
    [flipView addSubview:btn2];
    btn2.hidden = YES;
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:flipView];
    self.navigationItem.rightBarButtonItem = item;

}

#pragma mark - loadData

- (void)_loadData{
    MovieModelArray = [[NSMutableArray alloc]init];
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"us_box.json" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *subject = [dictionary objectForKey:@"subjects"];
    
    for (NSDictionary *dictionary in subject) {
        
        NSDictionary *subject = [dictionary objectForKey:@"subject"];
        MObj *model = [[MObj alloc]init];
        model.title = [subject objectForKey:@"title"];
        model.year = [subject objectForKey:@"year"];
        model.images = [subject objectForKey:@"images"];
        NSNumber *number = [[subject objectForKey:@"rating"]objectForKey:@"average"];
        model.average = [number floatValue];
        [MovieModelArray addObject:model];
    
    }
    _postView.movieModelArray =  MovieModelArray;
}

#pragma mark - tableInfoInMovieTable
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return MovieModelArray.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    movieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
    MObj *model = MovieModelArray[indexPath.row];
    cell.model = model;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
    
}

#pragma mark - action

- (void)flipAct:(UIButton *)button{
    
    UIView *flipView = self.navigationItem.rightBarButtonItem.customView;
    UIButton *btn1 = (UIButton*)[flipView viewWithTag:1];
    UIButton *btn2 = (UIButton*)[flipView viewWithTag:2];
    isLeft = !isLeft;
    [self flipViewAni:flipView isLeft:isLeft];
    [self flipViewAni:self.view isLeft:isLeft];
    btn1.hidden = !btn1.hidden;
    btn2.hidden = !btn2.hidden;
    _postView.hidden = !_postView.hidden;
    movieTable.hidden = !movieTable.hidden;
    
}

- (void)flipViewAni: (UIView*)view isLeft:(BOOL)ISLeft{
    
     UIViewAnimationOptions option = ISLeft?UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight;
    [UIView transitionWithView:view duration:0.5 options:option animations:nil completion:nil];
    
}

#pragma mark - main

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self _createNav];
    [self _createPosterView];
    [self _createMovieTable];
    [self _loadData];
    self.title = @"Movies";
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
